﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.QLNH_BUS_WS;
using IRemoting;

namespace QLNH.MyLayouts
{
    public partial class LayoutLogin : LayoutUC
    {
        public LayoutLogin(MainForm mainForm)
        {
            InitializeComponent();
            this.mainForm = mainForm;
        }

        #region Functions

        public override void Resize(Size size)
        {
            if (this.Size.Width < size.Width)
            {
                this.Location = new Point(size.Width / 2 - this.Size.Width / 2, size.Height / 2 - this.Size.Height / 2);
            }
            else
            {
                this.Location = new Point(0, 0);
            }
        }

        public override void OnReceivePackage(BasePackage package)
        {
            if (package.getLogginResult() == false)
            {
                string message = package.getLogginReplyMessage();
                lb_loginWarning.BeginInvoke((MethodInvoker)(() => lb_loginWarning.Text = message));
            }
            else
            {

                mainForm.pn_right.BeginInvoke((MethodInvoker)(() => mainForm.pn_right.Visible = true));

                string role = mainForm.CurrentUser.Role;

                if (role.Equals(Role.admin.ToString()))
                {
                    mainForm.SwictchLayout(LayoutKey.ADMIN_LAYOUT);
                }
                else if (role.Equals(Role.cashier.ToString()))
                {
                    mainForm.SwictchLayout(LayoutKey.CASHIER_LAYOUT);
                }
                else if (role.Equals(Role.chef.ToString()))
                {
                    mainForm.SwictchLayout(LayoutKey.CHEF_LAYOUT);
                }
                else if (role.Equals(Role.reception.ToString()))
                {
                    mainForm.SwictchLayout(LayoutKey.RECEPTION_LAYOUT);
                }
            }
        }

        //thuc hien dang nhap vao he thong
        public void Login(string username)
        {
            string result = mainForm.login(username);

            if (result != null)
            {
                lb_loginWarning.Text = result;
            }
        }

        #endregion

        #region Control Event

        private void bt_login_Click(object sender, EventArgs e)
        {
            if (this.tb_userName.Text.Trim().Equals(""))
            {
                this.lb_loginWarning.Text = "* Tên đăng nhập không hợp lệ, vui lòng nhập lại";
                return;
            }

            Login(this.tb_userName.Text.Trim());
        }

        private void tb_userName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.bt_login_Click(null, null);
            }
        }

        #endregion
    }
}
